package guet.dream.category;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSON;

public class CategoryService {

	 private CategoryDao categoryDao;
	 
	 public List<Map> buildTypeMap(int parentId){
		 List<Map> rootList = new ArrayList<Map>();
		 Category category = new Category();
		 category.setCategoryCode(parentId);
		 categoryDao = new CategoryDaoImpl();
		 Set<Category> categories = categoryDao.findChildCategories(category);
		 
		 for(Category cate:categories){
			 Map<String,Object> map = new HashMap<String, Object>();
			 Map<String,Object> attrMap = new HashMap<String, Object>();
			 Map<String,Object> dataMap = new HashMap<String, Object>();
			 Map<String,Object> dataAttrMap = new HashMap<String, Object>();
			 
			 attrMap.put("id", cate.getCategoryCode());
			 dataMap.put("title", cate.getName());
			   dataAttrMap.put("title", cate.getDescription());
			 
           dataMap.put("attr", dataAttrMap);   
		     map.put("attr", attrMap);
		     map.put("data", dataMap);
			 map.put("children", buildTypeMap(cate.getCategoryCode()));
			 rootList.add(map);
		 }
		 
		 return rootList;
	 }
	
	 public String categoryMapToJson(){
		 List<Map> list = buildTypeMap(0);
		 
		 return JSON.toJSONString(list);
	 }
}

